package com.sec.samsung.android.app.baymax.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.model.user.Account;
import com.sec.samsung.android.app.baymax.model.user.UserList;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;
import com.sec.samsung.android.app.baymax.utils.BaymaxUtils;
import com.sec.samsung.android.app.baymax.utils.SharePrefUtils;
import com.sec.samsung.android.app.baymax.view.adapter.ListHistoryAdapter;
import com.sec.samsung.android.app.baymax.view.adapter.ListHistoryAdapter.HistoryDeleteListener;

public class HistoryActivity extends Activity implements HistoryDeleteListener{
    // =============================Constant Define=====================
    // ============================Control Define =====================
    private ListView mHistoryListView;
    private ListHistoryAdapter mListAdapter;
    // ============================Variable Define =====================
    private UserList mUserList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiy_history);

        initLayout();
    }

    private void initLayout() {
        mUserList = BaymaxUtils.getUserList(this);
        mHistoryListView = (ListView) findViewById(R.id.list_history);
        mListAdapter = new ListHistoryAdapter(this, mUserList.userList);
        mListAdapter.setListener(this);
        mHistoryListView.setAdapter(mListAdapter);
        mHistoryListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Gson gson = new GsonBuilder().serializeNulls().create();
                Account user = mUserList.userList.get(position);
                String userJSON = gson.toJson(user);
                Intent intent = getIntent();
                intent.putExtra("return_data", userJSON);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void deleteHistory(int position) {
        mUserList.userList.remove(position);
        Gson gson = new GsonBuilder().serializeNulls().create();
        String userListJSON = gson.toJson(mUserList);
        SharePrefUtils.putStringValue(this, BaymaxConstant.KEY_USER_LIST_DATA, userListJSON);
        mListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClickItem(int position) {
        Gson gson = new GsonBuilder().serializeNulls().create();
        Account user = mUserList.userList.get(position);
        String userJSON = gson.toJson(user);
        Intent intent = getIntent();
        intent.putExtra("return_data", userJSON);
        setResult(RESULT_OK, intent);
        finish();
    }
}
